/// <summary>
// CodeFx.MySqlProviders - .NET Providers for MySql using the framework CodeFx
// Laurent Morisseau
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>


namespace CodeFx.MySqlProviders
{
    using System;
    using System.Text;
    using System.Web;
    using System.Data;
    using System.ComponentModel;
    using MySql.Data.MySqlClient;
    using System.Collections;
    using CodeFx;
    using CodeFx.Web;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Configuration.Provider;

    [DataObject]
    public static partial class Aspnet_membershipDTO
    {

        #region DTO Method

        #region Select
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public static Aspnet_membershipDAL Select(uint _UserId)
        {
            const string command = "select * from  aspnet_membership where UserId =?UserId";


            MySqlConnection conn = ConnectionString.Current.OpenConnection;

            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?UserId", _UserId));
            Aspnet_membershipDAL dataobject = null;
            try
            {
                dataobject = Select(cmd);
            }
            finally
            {
                conn.Close();
            }
            return dataobject;
        }

        #endregion

        #region SelectbyFk
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static List<Aspnet_membershipDAL> SelectByFk_ApplicationId(uint ApplicationId)
        {
            const string command = "select * from  aspnet_membership where ApplicationId =?ApplicationId";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;

            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", ApplicationId));
            List<Aspnet_membershipDAL> al = null;
            try
            {
                al = SelectAll(cmd);
            }
            finally
            {
                conn.Close();
            }
            return al;
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static List<Aspnet_membershipDAL> SelectByFk_UserId(uint UserId)
        {
            const string command = "select * from  aspnet_membership where UserId =?UserId";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;

            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?UserId", UserId));
            List<Aspnet_membershipDAL> al = null;
            try
            {
                al = SelectAll(cmd);
            }
            finally
            {
                conn.Close();
            }
            return al;
        }
        #endregion

        #region SelectAll
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static List<Aspnet_membershipDAL> SelectAll()
        {
            MySqlConnection conn = ConnectionString.Current.OpenConnection;

            MySqlCommand cmd = new MySqlCommand("select * from aspnet_membership", conn);
            List<Aspnet_membershipDAL> al = null;
            try
            {
                al = SelectAll(cmd);
            }
            finally
            {
                conn.Close();
            }
            return al;
        }
        #endregion

        #region Delete
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public static void Delete(Aspnet_membershipDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");

            const string command = "delete from aspnet_membership where UserId =?UserId";


            MySqlConnection conn = ConnectionString.Current.OpenConnection;
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?UserId", dataObject.UserId));

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

        #region ByFkDelete
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, false)]
        public static void DeleteByFk_ApplicationId(Aspnet_membershipDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");

            const string command = "delete from aspnet_membership where ApplicationId =?ApplicationId";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", dataObject.ApplicationId));


            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
            finally
            {
                conn.Close();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, false)]
        public static void DeleteByFk_UserId(Aspnet_membershipDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");

            const string command = "delete from aspnet_membership where UserId =?UserId";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?UserId", dataObject.UserId));


            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

        #region Insert
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public static void Insert(Aspnet_membershipDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");

            const string command = "insert into aspnet_membership (ApplicationId,UserId,Password,PasswordFormat,PasswordSalt,MobilePin,Email,PasswordQuestion,PasswordAnswer,IsApproved,IsLockedOut,CreateDate,LastLoginDate,LastPasswordChangedDate,LastLockedOutDate,FailedPasswordAttemptCount,FailedPasswordAttemptWindowStart,FailedPasswordAnswerAttemptCount,FailedPasswordAnswerAttemptWindowStart,Comment) values ( ?ApplicationId,?UserId,?Password,?PasswordFormat,?PasswordSalt,?MobilePin,?Email,?PasswordQuestion,?PasswordAnswer,?IsApproved,?IsLockedOut,?CreateDate,?LastLoginDate,?LastPasswordChangedDate,?LastLockedOutDate,?FailedPasswordAttemptCount,?FailedPasswordAttemptWindowStart,?FailedPasswordAnswerAttemptCount,?FailedPasswordAnswerAttemptWindowStart,?Comment)";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;

            MySqlCommand cmd = new MySqlCommand(command, conn);

            try
            {
                dataObject.Write(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion


        #region Update
        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public static void Update(Aspnet_membershipDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
            const string command = "update aspnet_membership set ApplicationId = ?ApplicationId,Password = ?Password,PasswordFormat = ?PasswordFormat,PasswordSalt = ?PasswordSalt,MobilePin = ?MobilePin,Email = ?Email,PasswordQuestion = ?PasswordQuestion,PasswordAnswer = ?PasswordAnswer,IsApproved = ?IsApproved,IsLockedOut = ?IsLockedOut,CreateDate = ?CreateDate,LastLoginDate = ?LastLoginDate,LastPasswordChangedDate = ?LastPasswordChangedDate,LastLockedOutDate = ?LastLockedOutDate,FailedPasswordAttemptCount = ?FailedPasswordAttemptCount,FailedPasswordAttemptWindowStart = ?FailedPasswordAttemptWindowStart,FailedPasswordAnswerAttemptCount = ?FailedPasswordAnswerAttemptCount,FailedPasswordAnswerAttemptWindowStart = ?FailedPasswordAnswerAttemptWindowStart,Comment = ?Comment where UserId = ?UserId";
            MySqlConnection conn = ConnectionString.Current.OpenConnection;
            MySqlCommand cmd = new MySqlCommand(command, conn);
            try
            {
                dataObject.Write(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

        #region Exist
        public static bool Exist(uint _UserId)
        {
            const string command = "select 1 from aspnet_membership where UserId =?UserId";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?UserId", _UserId));

            try
            {
                return (cmd.ExecuteScalar() != null);
            }
            catch (Exception ex)
            {
                throw new DALException(command, ex.InnerException);
            }
            finally
            {
                conn.Close();
            }
        }

        public static bool Exist(Aspnet_membershipDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");

            const string command = "select 1 from aspnet_membership where ApplicationId =?ApplicationId and UserId =?UserId and Password =?Password and PasswordFormat =?PasswordFormat and PasswordSalt =?PasswordSalt and MobilePin =?MobilePin and Email =?Email and PasswordQuestion =?PasswordQuestion and PasswordAnswer =?PasswordAnswer and IsApproved =?IsApproved and IsLockedOut =?IsLockedOut and CreateDate =?CreateDate and LastLoginDate =?LastLoginDate and LastPasswordChangedDate =?LastPasswordChangedDate and LastLockedOutDate =?LastLockedOutDate and FailedPasswordAttemptCount =?FailedPasswordAttemptCount and FailedPasswordAttemptWindowStart =?FailedPasswordAttemptWindowStart and FailedPasswordAnswerAttemptCount =?FailedPasswordAnswerAttemptCount and FailedPasswordAnswerAttemptWindowStart =?FailedPasswordAnswerAttemptWindowStart and Comment =?Comment";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", dataObject.ApplicationId));
            cmd.Parameters.Add(new MySqlParameter("?UserId", dataObject.UserId));
            cmd.Parameters.Add(new MySqlParameter("?Password", dataObject.Password));
            cmd.Parameters.Add(new MySqlParameter("?PasswordFormat", dataObject.PasswordFormat));
            cmd.Parameters.Add(new MySqlParameter("?PasswordSalt", dataObject.PasswordSalt));
            cmd.Parameters.Add(new MySqlParameter("?MobilePin", dataObject.MobilePin));
            cmd.Parameters.Add(new MySqlParameter("?Email", dataObject.Email));
            cmd.Parameters.Add(new MySqlParameter("?PasswordQuestion", dataObject.PasswordQuestion));
            cmd.Parameters.Add(new MySqlParameter("?PasswordAnswer", dataObject.PasswordAnswer));
            cmd.Parameters.Add(new MySqlParameter("?IsApproved", dataObject.IsApproved));
            cmd.Parameters.Add(new MySqlParameter("?IsLockedOut", dataObject.IsLockedOut));
            cmd.Parameters.Add(new MySqlParameter("?CreateDate", dataObject.CreateDate));
            cmd.Parameters.Add(new MySqlParameter("?LastLoginDate", dataObject.LastLoginDate));
            cmd.Parameters.Add(new MySqlParameter("?LastPasswordChangedDate", dataObject.LastPasswordChangedDate));
            cmd.Parameters.Add(new MySqlParameter("?LastLockedOutDate", dataObject.LastLockedOutDate));
            cmd.Parameters.Add(new MySqlParameter("?FailedPasswordAttemptCount", dataObject.FailedPasswordAttemptCount));
            cmd.Parameters.Add(new MySqlParameter("?FailedPasswordAttemptWindowStart", dataObject.FailedPasswordAttemptWindowStart));
            cmd.Parameters.Add(new MySqlParameter("?FailedPasswordAnswerAttemptCount", dataObject.FailedPasswordAnswerAttemptCount));
            cmd.Parameters.Add(new MySqlParameter("?FailedPasswordAnswerAttemptWindowStart", dataObject.FailedPasswordAnswerAttemptWindowStart));
            cmd.Parameters.Add(new MySqlParameter("?Comment", dataObject.Comment));
            try
            {
                return (cmd.ExecuteScalar() != null);
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
            finally
            {
                conn.Close();
            }

        }
        #endregion

        #endregion

        #region Public methods
        public static Aspnet_membershipDAL Select(MySqlCommand cmd)
        {
            if (cmd == null)
                throw new ArgumentNullException("cmd");
            Aspnet_membershipDAL dataObject = null;
            MySqlDataReader dr = null;
            try
            {
                dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
                if (dr.Read())
                {
                    dataObject = new Aspnet_membershipDAL();
                    dataObject.Read(dr);
                }
            }
            catch (Exception ex)
            {
                throw new DALException(cmd.CommandText, ex.InnerException);
            }
            finally
            {
                if (dr != null)
                    dr.Close();
            }
            return dataObject;
        }

        public static List<Aspnet_membershipDAL> SelectAll(MySqlCommand cmd)
        {
            if (cmd == null)
                throw new ArgumentNullException("cmd");
            List<Aspnet_membershipDAL> al = null;
            MySqlDataReader dr = null;
            try
            {
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    al = new List<Aspnet_membershipDAL>();
                    while (dr.Read())
                    {
                        Aspnet_membershipDAL dataObject = new Aspnet_membershipDAL();
                        dataObject.Read(dr);
                        al.Add(dataObject);
                    }
                    al.TrimExcess();
                }
            }
            catch (Exception ex)
            {
                throw new DALException(cmd.CommandText, ex.InnerException);
            }
            finally
            {
                if (dr != null)
                    dr.Close();
            }
            return al;
        }

        #endregion

    }
}